<?php

/**
 * Видеофайл
 *
 * @package    sf_sandbox
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class Type12Form extends BaseMaterialForm
{
  public function configure()
  {
  	
  		unset( 
  		$this['created_at'],  
  		$this['updated_at'],  
  		$this['user_id'], 
  		$this['see'], 
  		$this['click'], 
  		$this['country'],
  		$this['link'],
  		$this['custom_int1'],
  		$this['custom_int2'],
  		$this['custom_int3'],
  		$this['custom_text1'],
  		$this['custom_text2'],
  		$this['custom_text3'],
  		$this['custom_char1'],
  		$this['custom_char2'],
  		$this['stat'],
  		$this['city']);
  		
   		$c = new Criteria ( );	
		$c->addAnd ( FolderPeer::TREE_PARENT, 1, Criteria::NOT_EQUAL );
		$c->addAnd ( MaterialPeer::ID, 'tt' ); 	
  		
  		$this->setWidgets(array(
      'id'               => new sfWidgetFormInputHidden(),
  	  'parent_id'         => new sfWidgetParentMaterial(),	
  	   'title'               => new sfWidgetFormInput(array(),array('class'   =>  'material_title','style'=>'width:620px;')),	
  	  'type_id'           => new sfWidgetFormInputHidden(array(), array('value' =>'12', 'class'=>'test')),	 	 
  	  'photo_path' =>  new inputImageTag (  array (), array('mat_id'=>$this->object->getId(),'folder'=>'gallery') ), 
  	  'pub'      => new sfWidgetFormInputCheckbox(),
    ));
  		
    
         /////Ссылка на форум
		 if(sfConfig::get('app_use_link_forum')==true)
		 {
		 	$this->widgetSchema['custom_char3'] =  new sfWidgetFormInput(array(),array('size'=>'60'));
		 }
		 ////
		 
		 /////рейтинг
		 if(sfConfig::get('app_form_video_rating')==true)
		 {
		 	$this->widgetSchema['rating'] =  new sfWidgetFormInput(array(),array('size'=>'6'));
		 }
		 ////
		 
		 /////Описание
		 if(sfConfig::get('app_form_video_title')==true)
		 {
		 	$this->widgetSchema['title'] =  new sfWidgetFormInput(array(),array('size'=>50));
		 }
		 ////
		 
		  /////Заголовок
		 if(sfConfig::get('app_form_video_description')==true)
		 {
		 	$this->widgetSchema['short_content'] =  new sfWidgetFormTextarea(array(),array('class'   =>  'material_short_content','style'=>'width:620px'));
		 }
		 ////
    
  		
      if(sfConfig::get('app_use_simple_region')==true)
    {
    $this->widgetSchema['region_id'] =  new sfWidgetFormPropelChoice(array('multiple' => false, 'expanded' => true, 'model' => 'Region', 'add_empty' => false));
    }
    
  		$this->widgetSchema->setLabels(array(  
		'title'    => 'Заголовок',  
  		'parent_id'   => 'Принадлежность',
		'photo_path'   => 'Файл (формат avi, не более 5 мб)',  
  		'content'   => 'Содержимое',
  		'pub'   => 'Опубликован',
  		'region_id'   => 'Регион',
  		'seo_keyword'   => 'SEO слова для поиска',
  		'mat2_folder_list'   => 'Каталоги',
  		'custom_char3'   => 'Ссылка на форум: ',
  		'rating'   => 'Рейтинг',
		'short_content' => 'Короткое описание',));
  		
    
        $this->validatorSchema ['photo_path'] = new sfValidatorFile ( array ('required' => false, 
		'path' => sfConfig::get ( 'sf_web_dir' ) . '/uploads/material/video', 
        'mime_types'=>array('video/avi','video/x-msvideo','application/octet-stream', 'video/x-ms-wmv', 'video/x-ms-wmv','text/plain'),
		'max_size' => 20000000,
        'validated_file_class' => 'sfMatVideoValidatedFile' ),
        array ('invalid' => 'Invalid file.',
         'required' => 'Select a file to upload.', 
         'mime_types' => 'The file must be of AVI format.' )
        );
  		 $this->widgetSchema->setNameFormat('material[%s]');
  
  }
  
  
  
	
	
	public function saverec($par, $mat_id) {
		
		//echo $par->getId().'-'.$mat_id;
		$c = new Criteria ( );
		$c->add ( Mat2FolderPeer::FOLDER_ID, $par->getId () );
		$c->addAnd ( Mat2FolderPeer::MATERIAL_ID, $mat_id );
		$cnt = Mat2FolderPeer::doCount ( $c );
		if ($cnt == 0) {
			$re = new Mat2Folder ( );
			$re->setMaterialId ( $mat_id );
			$re->setFolderId ( $par->getId () );
			$re->save ();
		}
	
	}
  

  
  
public function save($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    try
    {
      $con->beginTransaction();

      $this->doSave($con);

      $con->commit();
    }
    catch (Exception $e)
    {
      $con->rollBack();

      throw $e;
    }

    $o = $this->getObject();
    $c = new Criteria();
    $c->add(Material2MaterialPeer::FROM_ID,$o->getParentId());
    $c->addAnd(Material2MaterialPeer::TO_ID,$o->getId());
    $cnt = Material2MaterialPeer::doCount($c);
    

    
    if($cnt==0 and $o->getParentId()>0)
    {    
  	$f = new Material2Material();
  	$f->setFromId($o->getParentId());
  	$f->setToId($o->getId());
  	$f->save();
    }
    return $this->object;
  }
  
}
